package com.yin.jia.liang.service;

import com.yin.jia.liang.dao.*;
import com.yin.jia.liang.entity.*;
import com.yin.jia.liang.vo.TeachPlVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeachPlService {

    @Autowired
    TeachPlMapper teachPlMapper;
    @Autowired
    TeachMapper teachMapper;
    @Autowired
    UserMapper userMapper;

    public List<TeachPlVo> findByUserId(int userId) {
        TeachPlExample ue = new TeachPlExample();
        TeachPlExample.Criteria criteria = ue.createCriteria();
        criteria.andUserIdEqualTo(userId);
        ue.setOrderByClause("id desc");
        List<TeachPl> g = teachPlMapper.selectByExampleWithBLOBs(ue);

        List<TeachPlVo> goodsPlVos = E2Vo(g);

        return goodsPlVos;
    }

    public List<TeachPlVo> findByTeachId(int goodsId) {
        TeachPlExample ue = new TeachPlExample();
        TeachPlExample.Criteria criteria = ue.createCriteria();
        criteria.andTeachIdEqualTo(goodsId);
        ue.setOrderByClause("id desc");
        List<TeachPl> g = teachPlMapper.selectByExampleWithBLOBs(ue);

        List<TeachPlVo> goodsPlVos = E2Vo(g);

        return goodsPlVos;
    }


    private List<TeachPlVo> E2Vo (List<TeachPl> goodsPls) {
        List<TeachPlVo> teachPlVos = new ArrayList<>();

        for (TeachPl g : goodsPls) {
            TeachPlVo vo = new TeachPlVo();

            Teach teach = teachMapper.selectByPrimaryKey(g.getTeachId());
            User user = userMapper.selectByPrimaryKey(g.getUserId());

            vo.setTeach(teach);
            vo.setUser(user);
            vo.setUserId(g.getUserId());
            vo.setId(g.getId());
            vo.setTeachId(g.getTeachId());
            vo.setCreateAt(g.getCreateAt());
            vo.setContent(g.getContent());

            teachPlVos.add(vo);
        }
        return teachPlVos;
    }

    public Integer save(TeachPl teachPl) {
        int i = teachPlMapper.insert(teachPl);
        return i;
    }



    public Integer delete(Integer id) {
        return teachPlMapper.deleteByPrimaryKey(id);
    }
}
